package com.mapproject.industrymap.controller;

import com.mapproject.industrymap.utils.ReturnValue;
import io.swagger.annotations.Api;
import it.geosolutions.geoserver.rest.GeoServerRESTManager;
import it.geosolutions.geoserver.rest.GeoServerRESTPublisher;
import it.geosolutions.geoserver.rest.decoder.RESTDataStore;
import it.geosolutions.geoserver.rest.decoder.RESTLayer;
import it.geosolutions.geoserver.rest.encoder.GSLayerEncoder;
import it.geosolutions.geoserver.rest.encoder.datastore.GSPostGISDatastoreEncoder;
import it.geosolutions.geoserver.rest.encoder.feature.GSFeatureTypeEncoder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.net.URL;
import java.util.List;

@RestController
@RequestMapping("/geopublish")
public class GeoserverPublishController {
    /**
     * 发布wms服务参数为待发布图层表名
     * @param tablename
     * @return
     */
    @RequestMapping(value = "/publishlayer")
    public ReturnValue getNewsmsg(@RequestParam(value ="tablename") String tablename) {
        //geoserver连接配置
        String RESTURL  = "http://192.168.220.1:8087/geoserver";
        String RESTUSER = "admin";
        String RESTPW   = "geoserver";
        //postGIS连接配置
        String postGisHost = "http://192.168.220.1";
        int postGisPort = 5432;
        String postGisUser = "postgres";
        String postGisPassWord = "wjgh123";
        String postGisDatabase = "industrymap";
        String result="";
        try {
            //File zipFile=new File("E:\\programs\\geoserverTest\\geoserverManager\\src\\main\\resources\\testdata\\shapefile\\cities.zip");
            //服务参数
            String ws = "wujin";
            String store_name = "wujin_zrzyhgh";
            String table_name = tablename;
            //判断workspace是否存在，若不存在则创建
            URL u = new URL(RESTURL);
            GeoServerRESTManager manager = new GeoServerRESTManager(u, RESTUSER, RESTPW);
            GeoServerRESTPublisher publisher = manager.getPublisher();
            List<String> workspaces = manager.getReader().getWorkspaceNames();
            if (!workspaces.contains(ws))
            {
                boolean creatws = publisher.createWorkspace(ws);
                System.out.println("create ws:"+creatws);
            }else
            {
                System.out.println("workspace已经存在:"+ws);
            }

            //判断datastore是否存在，若不存在则创建
            RESTDataStore restDataStore = manager.getReader().getDatastore(ws,store_name);
            if (restDataStore == null)
            {
                GSPostGISDatastoreEncoder store = new GSPostGISDatastoreEncoder(store_name);
                store.setHost(postGisHost);
                store.setPort(postGisPort);
                store.setUser(postGisUser);
                store.setPassword(postGisPassWord);
                store.setDatabase(postGisDatabase);
                store.setSchema("public");
                store.setConnectionTimeout(20);
                store.setMaxConnections(20);
                store.setMinConnections(1);
                store.setExposePrimaryKeys(true);
                boolean creatStore = manager.getStoreManager().create(ws,store);
                System.out.println("create datastore:"+creatStore);
            }else
            {
                System.out.println("数据存储已经存在:"+ store_name);
            }

            //判断layer是否存在，若不存在则创建
            RESTLayer layer = manager.getReader().getLayer(ws,table_name);
            if (layer == null)
            {
                GSFeatureTypeEncoder pds = new GSFeatureTypeEncoder();
                pds.setTitle(table_name);
                pds.setName(table_name);
                pds.setSRS("EPSG:4528");


                GSLayerEncoder layerEncoder = new GSLayerEncoder();
//                layerEncoder.setDefaultStyle("土地用途区样式");

                boolean publish = manager.getPublisher().publishDBLayer(ws,store_name,pds,layerEncoder);

                System.out.println("create publish:"+ publish);
            }else
            {
                System.out.println("已经发布:"+ table_name);
            }

        }catch (Exception mue){
            mue.printStackTrace();
        }

        return new ReturnValue(200,"图层已经发布成功");
    }
}
